package com.example.shiwu;

import static android.text.InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.shiwu.db.model.User;
import com.example.shiwu.service.UserService;

import java.util.List;

public class MyEditPassActivity extends AppCompatActivity{

    private Spinner spin_one;
    private Context mContext;
    private TextView line_old,line_old_val,oldpass,name_val,newpass_val,verify_val,line_new_val,line_verify_val;
    private Button old_pass_on,editpass_sub_btn;
    private EditText oldpass_val;
    //true为隐藏，false为显示
    private boolean mode = true;
    public TextView title_text,title_seting;
    public ImageView title_back;

    private UserService userService;
    private SharedPreferences preferences;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_edit_pass);
        name_val = findViewById(R.id.my_editpass_name_val);
        line_old = findViewById(R.id.my_below_line_old);
        line_old_val = findViewById(R.id.my_below_line_old_val);
        line_old.setBackgroundResource(R.drawable.line_bg);//密码错误 下划线变红
        line_old_val.setVisibility(View.GONE);//密码错误 展示提示
        
        old_pass_on = findViewById(R.id.old_pass_on);
        oldpass =findViewById(R.id.my_editpass_oldpass);
        oldpass_val = findViewById(R.id.my_editpass_oldpass_val);
        
        newpass_val = findViewById(R.id.my_editpass_newpass_val);
        verify_val = findViewById(R.id.my_editpass_verify_val);

        line_verify_val = findViewById(R.id.my_below_line_verify_val);
        line_new_val = findViewById(R.id.my_below_line_new_val);
        line_verify_val.setVisibility(View.GONE);
        line_new_val.setVisibility(View.GONE);

        title_back = findViewById(R.id.title_back);
        title_text = findViewById(R.id.title_text);
        title_seting = findViewById(R.id.title_setting);

        title_text.setText("修改密码");
        title_seting.setVisibility(View.INVISIBLE);

        editpass_sub_btn =findViewById(R.id.editpass_sub_btn);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);

        mContext = MyEditPassActivity.this;
        this.getUser();
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyEditPassActivity.this.finish();
            }
        });
        old_pass_on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mode){
                    oldpass_val.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    //为了点击之后输入框光标不变
                    oldpass_val.setSelection(oldpass_val.getText().length());
                    old_pass_on.setBackgroundResource(R.mipmap.login_icon_passwordon);
                    mode = !mode;
                }else {
                    oldpass_val.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    //为了点击之后输入框光标不变
                    oldpass_val.setSelection(oldpass_val.getText().length());
                    old_pass_on.setBackgroundResource(R.mipmap.login_icon_passwordoff);
                    mode = !mode;
                }

            }
        });
        editpass_sub_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upDataUser();
            }
        });


    }

    private void getUser(){
        userService = new UserService();
        String Name = preferences.getString("Name","");
        List<User> user = userService.getUser(Name);
        User us = user.get(0);
        name_val.setText(us.getName());
        oldpass_val.setText(us.getPassword());
    }
    
    private void upDataUser(){

        String newpass = newpass_val.getText().toString();
        String verify = verify_val.getText().toString();
        if(newpass.length() == 0){
            line_new_val.setVisibility(View.VISIBLE);
            line_new_val.setText("新密码不能为空");
        } else if (verify.length() == 0) {
            line_verify_val.setVisibility(View.VISIBLE);
            line_verify_val.setText("确认密码不能为空");
        }
        else if (newpass.length() == 0 && verify.length() == 0){
            line_new_val.setVisibility(View.VISIBLE);
            line_new_val.setText("新密码不能为空");
            line_verify_val.setVisibility(View.VISIBLE);
            line_verify_val.setText("确认密码不能为空");
        }
        else if (newpass.length()>0 && verify.length()>0) {
            if (!newpass.equals(verify)) {
                line_verify_val.setVisibility(View.VISIBLE);
                line_verify_val.setText("新密码与确认密码不一致");
            }else{
                String name = preferences.getString("Name","");
                User user = new User();
                user.setPassword(verify);
                user.setName(name);
                userService.updata(user);
            }
        }


    }
    


}
